package app.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.filechooser.FileFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import app.model.IMDBModel;
import app.util.DHTConnector;
import app.view.IMDBView;

public class IMDBController {
	private IMDBModel model;
	private IMDBView view;
	
	public IMDBController(){
		model = new IMDBModel(this);
		view = new IMDBView();
		
		addActionListeners();
	}
	
	private void addActionListeners(){
		view.getLoadDataset().addActionListener(new CustomActionListener(this));
		view.getSearchButton().addActionListener(new CustomActionListener(this));
		view.getConnectButton().addActionListener(new CustomActionListener(this));
	}
	
	class CustomActionListener implements ActionListener{
		private IMDBController context;
		
		public CustomActionListener(IMDBController context){
			this.context = context;
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			//load file
			if(e.getSource().equals(((IMDBView)(context.getView())).getLoadDataset()))
				context.openDataFile();
			//send get request
			else if(e.getSource().equals(((IMDBView)(context.getView())).getSearchButton()))
				context.getRequest(((IMDBView)(context.getView())).getSearchInputField().getText());
			//connect to the DHT
			else if(e.getSource().equals(((IMDBView)(context.getView())).getConnectButton()))
				context.connectToDHT(((IMDBView)(context.getView())).getConnectInput().getText());
		}
		
	}
	

	public void openDataFile() {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setDialogTitle("Select dataset file");

		//File filter
		FileFilter f = new FileFilter(){

			@Override
			public boolean accept(File f) {
				return f.getName().toLowerCase().endsWith(".list") || f.isDirectory();
			}
			
			@Override
			public String getDescription(){
				return "List-Files (.list)";
			}
			
		};
		fileChooser.setFileFilter(f);
		int returnVal = fileChooser.showOpenDialog(view);
		
		if(returnVal == JFileChooser.APPROVE_OPTION){
			String fileName = fileChooser.getSelectedFile().getAbsolutePath();
			model.fileSelected(fileName);
			
			view.activateProgressBar("Loading data...");
		}
	}
	
	public void connectToDHT(String hostName) {
		view.getStatusLabel().setText("");
		//connect to the DHT
		if(DHTConnector.connectToDHT(hostName)){
			//connection successfully established
			view.getStatusLabel().setText("Connected to : " + hostName);
		}
		else{
			//connection failed
			JOptionPane.showMessageDialog(null, "Connection to \"" + hostName + "\" failed", "Connection Error", JOptionPane.INFORMATION_MESSAGE);
		}
	}

	public void getRequest(String key) {
		//clear the result list of the view
		view.getResultTable().setModel(new DefaultTableModel(null, new String[]{"No results found for \"" + key + "\""}));
		model.getRequest(key);
	}

	public Object getView() {
		return view;
	}

	public void dataImportFinished(){
		view.deactivateProgressBar("Finished loading data");
	}
	
	public static void main(String args[]){
		IMDBController controller = new IMDBController();
	}

	public void answerGetRequest(String key, String[] values) {
		//no values found
		if(values == null){
			
		} else {
			String[][] resultRows = new String[values.length][1];
			for(int i = 0; i < values.length; i++){
				resultRows[i][0] = values[i];
			}
			TableModel resultModel = new DefaultTableModel(resultRows, new String[]{"Search Results for \"" + key + "\""});
			view.getResultTable().setModel(resultModel);
		}
	}
	
	

}
